package com.controller.broker;

import com.dao.Flight;
import com.rmi.RMIInterface;
import com.rmi.RMITLSClientSocketFactory;
import com.util.Constant;
import com.util.Printer;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.List;

/**
 * Created by Phillip on 2014/4/15.
 */
public class BrokerRMIClient {

    private Registry registry;
    private RMIInterface stub;
    private Logger logger;
    public BrokerRMIClient() {

        try {
            logger = Logger.getLogger(BrokerRMIClient.class);
            PropertyConfigurator.configure(Constant.BROKER_PRO_STRING);

            registry = LocateRegistry.getRegistry("localhost", 1099,
                    new RMITLSClientSocketFactory(Constant.SER_KEYSTORE_STRING, Constant.CLI_KEYSTORE_STRING, "huyifei"));

            Printer.p(registry.toString());

            String[] strings = registry.list();
            for (String string : strings) {
                Printer.p(string);
            }
            stub = (RMIInterface) registry.lookup(Constant.RMISERVER_STRING);
            logger.debug("get the stub");

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
    }

    public List<Flight> queryFlights(String deptString, String destString, String deptDateString, String returnDateString) throws RemoteException {
        logger.debug("queryFlights1 is invoked");
        return stub.queryFlights(deptString, destString, deptDateString, returnDateString);
    }

    public List<String> queryCity() throws RemoteException {
        logger.debug("queryCity1 is invoked");
        return stub.queryCity();
    }

    public String book(String flightNoString1, String deptString, String clasString1, String flightNoString2, String returnString, String classString2, String nameString, String creditString) throws RemoteException {
        logger.debug("book1 is invoked");
        return stub.book(flightNoString1, deptString, clasString1, flightNoString2, returnString, classString2, nameString, creditString);
    }

    public String queryRates(String flightString, String classString) throws RemoteException {
        logger.debug("queryRates1 is invoked");
        return stub.queryRates(flightString, classString);
    }

    public String queryAirline(String deptString, String dsctString) throws RemoteException {
        logger.debug("queryAirline1 is invoked");
        return stub.queryAirline(deptString, dsctString);
    }
}
